package com.jh.paymentchannel.payment.alipay;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConstants;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author Yichuan
 * @version 1.0
 */
@Component
public class AlipayConfig {

    /**
     * 支付网关
     */
    @Value("${alipay.serverUrl}")
    private String serverUrl;

    /**
     * 应用授权URL
     */
    @Value("${alipay.authUrl}")
    private String authUrl;

    /**
     * 支付异步回调地址
     */
    @Value("${alipay.notifyUrl}")
    private String notifyUrl;

    /**
     * 支付同步回调地址
     */
    @Value("${alipay.returnUrl}")
    private String returnUrl;

    /**
     * 授权回调地址
     */
    @Value("${alipay.appAuthUrl}")
    private String appAuthUrl;

    /**
     * 应用id(服务商)
     */
    @Value("${alipay.appId}")
    private String appId;

    /**
     * pid
     */
    @Value("${alipay.pid}")
    private String pid;

    /**
     * 应用的私钥(服务商)
     */
    @Value("${alipay.privateKey}")
    private String privateKey;

    @Value("${alipay.alipayPubKey}")
    private String alipayPubKey;

    /**
     * 子商户appId
     */
    @Value("${alipay.mAppId}")
    private String mAppId;

    /**
     * 子商户的商户号
     */
    @Value("${alipay.mPid}")
    private String mPid;

    /**
     * 子商户的私钥
     */
    @Value("${alipay.mPrivateKey}")
    private String mPrivateKey;

    /**
     * 子商户的支付宝公钥
     */
    @Value("${alipay.mAlipayPubKey}")
    private String mAlipayPubKey;

    public String getServerUrl() {
        return serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public String getAuthUrl() {
        return authUrl;
    }

    public void setAuthUrl(String authUrl) {
        this.authUrl = authUrl;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    public String getAppAuthUrl() {
        return appAuthUrl;
    }

    public void setAppAuthUrl(String appAuthUrl) {
        this.appAuthUrl = appAuthUrl;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    public String getAlipayPubKey() {
        return alipayPubKey;
    }

    public void setAlipayPubKey(String alipayPubKey) {
        this.alipayPubKey = alipayPubKey;
    }

    public String getmAppId() {
        return mAppId;
    }

    public void setmAppId(String mAppId) {
        this.mAppId = mAppId;
    }

    public String getmPid() {
        return mPid;
    }

    public void setmPid(String mPid) {
        this.mPid = mPid;
    }

    public String getmPrivateKey() {
        return mPrivateKey;
    }

    public void setmPrivateKey(String mPrivateKey) {
        this.mPrivateKey = mPrivateKey;
    }

    public String getmAlipayPubKey() {
        return mAlipayPubKey;
    }

    public void setmAlipayPubKey(String mAlipayPubKey) {
        this.mAlipayPubKey = mAlipayPubKey;
    }

    public AlipayClient merchantAlipayClient() {
        return new DefaultAlipayClient(serverUrl, mAppId, mPrivateKey, AlipayConstants.FORMAT_JSON,
                AlipayConstants.CHARSET_UTF8, mAlipayPubKey, AlipayConstants.SIGN_TYPE_RSA2);
    }

    /**
     * 拼凑授权请求URL
     */
    public String pieceAppToAuthUrl(String shopId) {
        return String.format("%s?app_id=%s&redirect_uri=%s?shop_id=%s", authUrl, appId, appAuthUrl, shopId);
    }
}
